package com.tebiecloud.base.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.base.client.model.StorefVo;
import com.tebiecloud.base.client.model.entity.BaseAccount;
import com.tebiecloud.base.client.model.entity.BaseUser;
import com.tebiecloud.base.server.service.BaseAccountService;
import com.tebiecloud.base.server.service.BaseUserService;
import com.tebiecloud.base.server.service.feign.StoreServiceClient;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import com.tebiecloud.store.client.model.entity.Store;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 登录账号 前端控制器
 *
 * @author tebie
 * @date 2019-08-02
 */
@RestController
@RequestMapping("baseaccount")
public class BaseAccountController {

    @Autowired
    private BaseAccountService baseAccountService;

    @Autowired
    StoreServiceClient storeServiceClient;

    @Autowired
    BaseUserService baseUserService;

    /**
     * 查询角色为agm的
     */
    @PostMapping("agm")
    public ResultBody agmget(
            @RequestParam(value = "page", required = false, defaultValue = "1") int page,
            @RequestParam(value = "limit", required = false, defaultValue = "10") int limit) {
        List<StorefVo> agm = baseAccountService.agm(page, limit);
        if (agm == null) return ResultBodyUtils.failed();
        for (StorefVo storefVo : agm) {
            Long userId = storefVo.getUserId();
            Integer count = storeServiceClient.count(userId);
            if (count == null) return ResultBodyUtils.failed();
            storefVo.setStores(count);
        }
        return ResultBodyUtils.ok(agm);
    }

    /**
     * 查询agm详细信息
     */
    @PostMapping("/agmxx")
    public ResultBody agmxx(@RequestParam("userId") Long userId) {
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<BaseUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        BaseUser one = baseUserService.getOne(queryWrapper);
        if (one == null) return ResultBodyUtils.failed();
        List<Store> agmxx = storeServiceClient.agmxx(userId);
        if (agmxx == null) return ResultBodyUtils.failed();
        map.put("one", one);
        map.put("agmxx", agmxx);
        return ResultBodyUtils.ok(map);
    }

    /**
     * 获取分页数据
     *
     * @param map
     * @return
     */
    @GetMapping(value = "/list")
    public ResultBody<IPage<BaseAccount>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        BaseAccount query = pageParams.mapToObject(BaseAccount.class);
        QueryWrapper<BaseAccount> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(baseAccountService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    public ResultBody<BaseAccount> get(@RequestParam("id") Long id) {
        BaseAccount entity = baseAccountService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     *
     * @param userId      用户Id,
     * @param account     账号标识：手机号、邮箱、 用户名、或第三方应用的唯一标识,
     * @param password    密码凭证：站内的保存密码、站外的不保存或保存token）,
     * @param accountType 账号类型:password-密码、mobile-手机号、email-邮箱、mpweixin-微信、weibo-微博、qq-等等,
     * @param domain      账户域:@admin.com,@developer.com,
     * @param registerIp  注册IP,
     * @param status      状态:0-禁用 1-启用 2-锁定
     * @return
     */
    @PostMapping("/add")
    public ResultBody add(
            @RequestParam(value = "userId", required = false) Long userId,
            @RequestParam(value = "account", required = false) String account,
            @RequestParam(value = "password", required = false) String password,
            @RequestParam(value = "accountType", required = false) String accountType,
            @RequestParam(value = "domain", required = false) String domain,
            @RequestParam(value = "registerIp", required = false) String registerIp,
            @RequestParam(value = "status", required = false) Integer status
    ) {
        BaseAccount entity = new BaseAccount();
        entity.setUserId(userId);
        entity.setAccount(account);
        entity.setPassword(password);
        entity.setAccountType(accountType);
        entity.setDomain(domain);
        entity.setRegisterIp(registerIp);
        entity.setStatus(status);
        baseAccountService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     *
     * @param userId      用户Id
     * @param account     账号标识：手机号、邮箱、 用户名、或第三方应用的唯一标识
     * @param password    密码凭证：站内的保存密码、站外的不保存或保存token）
     * @param accountType 账号类型:password-密码、mobile-手机号、email-邮箱、mpweixin-微信、weibo-微博、qq-等等
     * @param domain      账户域:@admin.com,@developer.com
     * @param registerIp  注册IP
     * @param status      状态:0-禁用 1-启用 2-锁定
     * @return
     */
    @PostMapping("/update")
    public ResultBody update(
            @RequestParam(value = "accountId", required = false) Long accountId,
            @RequestParam(value = "userId", required = false) Long userId,
            @RequestParam(value = "account", required = false) String account,
            @RequestParam(value = "password", required = false) String password,
            @RequestParam(value = "accountType", required = false) String accountType,
            @RequestParam(value = "domain", required = false) String domain,
            @RequestParam(value = "registerIp", required = false) String registerIp,
            @RequestParam(value = "status", required = false) Integer status
    ) {
        BaseAccount entity = new BaseAccount();
        entity.setAccountId(accountId);
        entity.setUserId(userId);
        entity.setAccount(account);
        entity.setPassword(password);
        entity.setAccountType(accountType);
        entity.setDomain(domain);
        entity.setRegisterIp(registerIp);
        entity.setStatus(status);
        baseAccountService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     *
     * @param id  单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("/remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if (id != null && id > 0) {
            baseAccountService.removeById(id);
        }
        if (ids != null && ids.length() > 0) {
            baseAccountService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
